import axios from 'axios'

const API = {
  // 用户登录
  login: (username, password) => {
    const params = new URLSearchParams()
    params.append('username', username)
    params.append('password', password)

    return axios.post('/douyin/user/login/', params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  },

  // 用户注册  
  register: (username, password) => {
    const params = new URLSearchParams()
    params.append('username', username)
    params.append('password', password)

    return axios.post('/douyin/user/register/', params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  },

  // 获取用户信息
  getUserInfo: (userId, token) => {
    return axios.get('/douyin/user/', {
      params: { user_id: userId.toString(), token }
    })
  },

  // 获取视频流
  getFeed: (latestTime, token) => {
    const params = { latest_time: latestTime }
    if (token) {
      params.token = token
    }
    return axios.get('/douyin/feed', { params })
  },

  // 视频点赞/取消点赞
  toggleFavorite: (token, videoId, actionType) => {
    const params = new URLSearchParams()
    params.append('token', token)
    params.append('video_id', videoId.toString())
    params.append('action_type', actionType)

    return axios.post('/douyin/favorite/action/', params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  },

  // 发布视频
  publishVideo: (token, data, title) => {
    const formData = new FormData()
    formData.append('token', token)
    formData.append('data', data)
    formData.append('title', title)
    return axios.post('/douyin/publish/action/', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 获取用户发布的视频列表
  getUserVideos: (token, userId) => {
    return axios.get('/douyin/publish/list/', {
      params: { token, user_id: userId.toString() }
    })
  },

  // 删除视频
  deleteVideo: (token, videoId) => {
    // 使用DELETE方法，确保URL和参数名称与后端完全匹配
    const url = `/douyin/publish/delete/?token=${token}&video_id=${videoId.toString()}`
    return axios.delete(url)
  }
}

export default API 